﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nes4Net.UI
{
    public class Utils
    {
        /// <summary>
        /// Convert Byte[] to Image
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Image Int32ToImage(Int32[] buffer)
        {
            var bm = new Bitmap(256, 240);
            for (var i = 0; i < buffer.Length; i++)
            {
                var arr = BitConverter.GetBytes(buffer[i]);
                var color = Color.FromArgb(255, arr[0], arr[1], arr[2]);
                //var r = buffer[i] & 0x000000FF;
                //var g = (buffer[i] & 0x0000FF00) >> 8;
                //var b = (buffer[i] & 0x00FF0000) >> 16;
                //var color = Color.FromArgb(255, r, g, b);
                var x = i % 256;
                var y = i / 256;
                bm.SetPixel(x, y, color);
            }
            return bm;
        }

        public static Byte[] ReadFile2(String path)
        {
            using (var reader = File.OpenRead(path))
            {
                var buff = new Byte[reader.Length];
                reader.Read(buff, 0, buff.Length);
                return buff;
            }
        }
    }
}
